import { http, type HttpResponse } from '@/utils/request';
export interface EnumParams {
  key?: string;
}

export interface EnumItem {
  dictCode?: number | string | null;
  dictSort?: number | string | null;
  dictLabel?: string | null;
  dictValue?: string | null;
  dictType?: string | null;
  [key: string]: unknown;
}

export interface UploadPayload {
  file: File;
}

export interface UploadResult {
  url?: string | null;
  fileName?: string | null;
}

/**
 * 获取枚举
 * 默认会拉取订单相关所需的全部字典
 */
export async function fetchEnum(
  params?: EnumParams,
): Promise<HttpResponse<Record<string, EnumItem[]>>> {
  // 如果调用方未传 key，则默认取订单相关的全部枚举
  const payload =
    params && params.key
      ? params
      : {
          key: [
            'sp_order_pay_channel',
            'sp_order_pay_method',
            'sp_order_pay_type',
            'sp_order_status',
            'id_type',
            'supplement_status',
            'sp_order_review_status',
            'sp_order_offline_pay_method'
          ],
        };

  return await http.post<Record<string, EnumItem[]>>('/common/getEnum', payload);
}

/**
 * 文件上传
 */
export async function uploadFile(file: File): Promise<UploadResult | null> {
  const formData = new FormData();
  formData.append('file', file);
  
  // 注意：使用 FormData 时，不要手动设置 Content-Type
  // 让浏览器/axios 自动设置，包括 boundary 参数
  // 手动设置会导致缺少 boundary，从而引发网络错误
  const response = await http.post<UploadResult>('/common/upload', formData);
  // axios 拦截器已经处理了响应，返回的是 HttpResponse<UploadResult>
  // response.data 就是 UploadResult 类型的数据
  return (response as UploadResult) || null;
}

// 导出订单相关 API
export * from './order';
